var express = require('express')
var router = express.Router(),
    rp = require('request-promise'),
    debug = require('debug')('react-demo')

var configFile = 'config.'+process.env.NODE_ENV+'.json'
var config = require('../../config/'+configFile);

    router.post("/", function(req, res, next){
      var token = req.cookies.access_token
      var option = {
        method: 'POST',
        uri: config.apiHost+'/api/roles/queryRoles',
        body: req.body,
        headers: {
          authorization: 'Bearer '+token
        },
        json: true
      }

      rp(option)
        .then(function(result) {
          debug(result);
          if (result.success) {
            return res.json(result)
          }else {
            return res.status(500).json({
              success: false,
              errMsg: result.errMsg
            })
          }
        })
        .catch(function(err) {
          console.error(err);
          return res.status(err.statusCode).json({
            success: false,
            errCode: err.statusCode,
            errMsg: err.error?err.error.errMsg:err
          })
        })
    })

    router.post("/create", function(req, res, next){
      var token = req.cookies.access_token;
      var option = {
        method: 'POST',
        uri: config.apiHost+'/api/roles/create',
        body: req.body,
        headers: {
          authorization: 'Bearer '+token
        },
        json: true
      }

      rp(option)
        .then(function(result) {
          debug(result);
          if (result.success) {
            return res.json(result)
          }else {
            return res.status(500).json({
              success: false,
              errMsg: result.errMsg
            })
          }
        })
        .catch(function(err) {
          console.error(err);
          return res.status(err.statusCode).json({
            success: false,
            errCode: err.statusCode,
            errMsg: err.error?err.error.errMsg:err
          })
        })
    })

    router.post("/update", function(req, res, next){
      console.log(req.body);
      var token = req.cookies.access_token
      var option = {
        method: 'POST',
        uri: config.apiHost+'/api/roles/update',
        body: req.body,
        headers: {
          authorization: 'Bearer '+token
        },
        json: true
      }

      rp(option)
        .then(function(result) {
          debug(result);
          if (result.success) {
            return res.json(result)
          }else {
            return res.status(500).json({
              success: false,
              errMsg: result.errMsg
            })
          }
        })
        .catch(function(err) {
          console.error(err);
          return res.status(err.statusCode).json({
            success: false,
            errCode: err.statusCode,
            errMsg: err.error?err.error.errMsg:err
          })
        })
    })

    router.post("/querySysUsers", function(req, res, next){
      console.log(req.body);
      var token = req.cookies.access_token
      var option = {
        method: 'POST',
        uri: config.apiHost+'/api/roles/querySysUsers',
        body: req.body,
        headers: {
          authorization: 'Bearer '+token
        },
        json: true
      }

      rp(option)
        .then(function(result) {
          debug(result);
          if (result.success) {
            return res.json(result)
          }else {
            return res.status(500).json({
              success: false,
              errMsg: result.errMsg
            })
          }
        })
        .catch(function(err) {
          console.error(err);
          return res.status(err.statusCode).json({
            success: false,
            errCode: err.statusCode,
            errMsg: err.error?err.error.errMsg:err
          })
        })
    })

    router.get("/delete", function(req, res, next){

      var token = req.cookies.access_token;
      var option = {
        method: 'get',
        uri: config.apiHost+'/api/roles/delete?id='+req.query.id,
        headers: {
          authorization: 'Bearer '+token
        },
        json: true
      }

      rp(option)
        .then(function(result) {
          debug(result);
          if (result.success) {
            return res.json(result)
          }else {
            return res.status(500).json({
              success: false,
              errMsg: result.errMsg
            })
          }
        })
        .catch(function(err) {
          console.error(err);
          return res.status(err.statusCode).json({
            success: false,
            errCode: err.statusCode,
            errMsg: err.error?err.error.errMsg:err
          })
        })
    })

    router.post("/deleteRoles", function(req, res, next){

      var token = req.cookies.access_token;
      var option = {
        method: 'post',
        uri: config.apiHost+'/api/roles/deleteRoles',
        body: req.body,
        headers: {
          authorization: 'Bearer '+token
        },
        json: true
      }

      rp(option)
        .then(function(result) {
          debug(result);
          if (result.success) {
            return res.json(result)
          }else {
            return res.status(500).json({
              success: false,
              errMsg: result.errMsg
            })
          }
        })
        .catch(function(err) {
          console.error(err);
          return res.status(err.statusCode).json({
            success: false,
            errCode: err.statusCode,
            errMsg: err.error?err.error.errMsg:err
          })
        })
    })

    module.exports = router
